﻿Imports Microsoft.VisualBasic
Imports System.Drawing.Design
Imports System.Windows.Forms.Design
Imports System

   Public Class NIB_RangeCheckMinMaxPropertiesEditor
      Inherits UITypeEditor

      'Get an IWindowsFormsEditorService object.
      Dim editorservice As IWindowsFormsEditorService = Nothing
      Public Overrides Function EditValue( _
         ByVal context As System.ComponentModel.ITypeDescriptorContext, _
         ByVal provider As System.IServiceProvider, _
         ByVal value As Object) As Object

      If (provider IsNot Nothing) Then
         editorservice = _
            CType(provider.GetService(GetType(IWindowsFormsEditorService)),  _
            IWindowsFormsEditorService)
      End If

      Dim currentValue As ValueType = CType(value, ValueType)

      If editorservice IsNot Nothing Then

         ' Make the editing control.
         Dim editorcontrol As New frmEditWithSetDefault

         If context IsNot Nothing Then

            Dim fi As Reflection.FieldInfo = context.Instance.GetType.GetField("Parent", Reflection.BindingFlags.GetField Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
            Dim nib As Object = fi.GetValue(context.Instance) 'Parent is the NIB
            Dim rangecheck As Object = Reflectionhelpers.GetPropPublic("RangeChecking", nib)
            Dim maxval As ValueType = CType(Reflectionhelpers.GetPropPublic("MaxValue", rangecheck), ValueType)
            Dim minval As ValueType = CType(Reflectionhelpers.GetPropPublic("MinValue", rangecheck), ValueType)

            'Dim systype As Type = CType(Reflectionhelpers.GetPropNonPublic("SystemType", nib), Type)
            Dim SystemTypeCode As TypeCode = CType(Reflectionhelpers.GetPropPublic("SystemTypeCode", nib), System.TypeCode)

            Dim nibval As ValueType = CType(Reflectionhelpers.GetPropPublic("Value", nib), ValueType)

            With editorcontrol
            Select Case context.PropertyDescriptor.Name
               Case "MinValue"
                  .DefaultSetting = frmEditWithSetDefault.DefaultTypeSetting.Min
               Case "MaxValue"
                  .DefaultSetting = frmEditWithSetDefault.DefaultTypeSetting.Max

               Case Else
                  Throw New Exception("Error determining calling property in MinMaxValueEditor")
            End Select
            .SystemTypeCode = SystemTypeCode
            '.systemType = systype

            .Value = CType(value, ValueType)

            .Button1.Text = "Set " & context.PropertyDescriptor.DisplayName & " to " & _
                            value.GetType.Name & " MinValue"

            .Visible = True
            .CloseRoutine = AddressOf Me.CloseDropDownWindow

            End With

            ' Display the editing control.

            editorservice.DropDownControl(editorcontrol)
            'editorservice.ShowDialog(editorcontrol)
            ' Save the new results.

            Dim editorValuePasses As Boolean = True
            Dim AIsGtB As Reflection.MethodInfo
            AIsGtB = editorcontrol.nib.GetType.GetMethod("A_IsGreaterThan_B", _
                                                         Reflection.BindingFlags.Static Or _
                                                         Reflection.BindingFlags.NonPublic)

            Select Case context.PropertyDescriptor.Name
               Case "MinValue"

                  If CBool(AIsGtB.Invoke(editorcontrol.nib, _
                                    New Object() _
                                       {editorcontrol.Value, _
                                        nibval, _
                                        SystemTypeCode})) Then
                     MsgBox("Min Value cannot be greater than current value")
                     editorValuePasses = False

                  ElseIf CBool(AIsGtB.Invoke(editorcontrol.nib, _
                                    New Object() _
                                       {editorcontrol.Value, _
                                        maxval, _
                                        SystemTypeCode})) Then

                     MsgBox("Min Value cannot be greater than max value")

                     editorValuePasses = False

                  End If


               Case "MaxValue"
                  If CBool(AIsGtB.Invoke(editorcontrol.nib, _
                                    New Object() _
                                       {nibval, _
                                       editorcontrol.Value, _
                                        SystemTypeCode})) Then

                     MsgBox("Max Value cannot be less than current value")
                     editorValuePasses = False

                  ElseIf CBool(AIsGtB.Invoke(editorcontrol.nib, _
                                    New Object() _
                                       {minval, _
                                       editorcontrol.Value, _
                                        SystemTypeCode})) Then

                     MsgBox("Max Value cannot be less than Min value")
                     editorValuePasses = False
                  End If
               Case Else
                  Throw New Exception("Error determining calling property in MinMaxValueEditor")
            End Select

            If editorValuePasses Then
               Return editorcontrol.Value
            Else
               Return currentValue
            End If
         End If ' context isnot Nothing 

         End If 'editorservice Isnot Nothing

         Return MyBase.EditValue(context, provider, value)
      End Function

      Public Overrides Function GetEditStyle( _
            ByVal context As System.ComponentModel.ITypeDescriptorContext) _
            As System.Drawing.Design.UITypeEditorEditStyle

         Return UITypeEditorEditStyle.DropDown
      End Function

      Public Sub CloseDropDownWindow(ByVal sender As Object, ByVal e As EventArgs)
         editorservice.CloseDropDown()
      End Sub
   End Class